/*
    hisportdriver.h - HiSport driver interface header file
    Copyright (C) 1999-2004  Frodo Looijaard <frodol@dds.nl>, and
                             Mark D. Studebaker <mdsxyz123@yahoo.com>
    Copyright (C) 2004-2012  Jean Delvare <khali@linux-fr.org>
    Copyright (C) 2024  Huawei Technologies Co., Ltd.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301 USA.
*/

#ifndef HISPORTDRIVER_H
#define HISPORTDRIVER_H

#include <stdint.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

#define HISPORT_DEVICE_NAME "/dev/hisport"

#define HISPORT_CMD_MAGIC 'h'
#define HISPORT_CMD_INIT _IOW(HISPORT_CMD_MAGIC, 0, int)
#define HISPORT_CMD_WRITE _IOW(HISPORT_CMD_MAGIC, 1, int)
#define HISPORT_CMD_READ _IOWR(HISPORT_CMD_MAGIC, 2, int)


/* Forward declarations */
struct hisport_data_s;

int32_t hisport_open(void);
int32_t hisport_init(int fd);
int32_t hisport_close(int fd);
int32_t hisport_read(int fd, struct hisport_data_s *data_in);

struct hisport_init_s
{
    uint32_t bus_num;       // HiSport总线编号
    uint32_t reg_num;       // 寄存器编号
    uint32_t flag_num;      // 标志位编号
    uint32_t send_reg_addr; // 发送寄存器地址
    uint32_t recv_reg_addr; // 接收寄存器地址
    uint32_t flag_reg_addr; // 接收寄存器值
    uint32_t max_batch_len; // 最大批量长度
    uint32_t max_data_len;  // 最大数据长度
    uint32_t max_offset_width; // 最大偏移量宽度
    uint32_t max_length_width; // 最大长度宽度
};

/* 传输参数结构体（读写使用同一个结构体）I2C_CMD_READ 和 I2C_CMD_WRITE 的参数*/
struct hisport_data_s
{
    uint32_t bus_id;       // HiSport端口编号
    uint32_t addr;         // 地址
    uint32_t offset;       // 偏移量
    uint8_t *tx_data;      // 发送数据缓冲区
    uint32_t tx_data_len;  // 发送数据长度
    uint8_t *rx_data;      // 接收数据缓冲区
    uint32_t rx_data_len;  // 接收数据长度
    uint8_t offset_width;  // 偏移量宽度
    uint8_t data_width;    // 数据宽度
    uint8_t rw_flag;       // 读写标志
    uint32_t read_cnt;      // 读取次数
    uint32_t write_cnt;     // 写入次数
    uint8_t delay_ms;      // 延时时间
};

#endif